<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <el-upload
            action="/upload"
            name="picFile"
            limit="9"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :on-success="handleSuccess">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <input type="text" placeholder="说点啥？" v-model="weibo.content">
    <input type="button" value="发微博" @click="insert()">
</div>
</body>
<!--引入axios和vue框架文件-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return{
                dialogImageUrl: '',
                dialogVisible: false,
                weibo:{content:"",urls:""},
                fileList:[]
            }
        },
        methods: {
            insert(){
                //得到多张图片的途径合并成一个字符串，赋值给urls
                if (v.fileList.length == 0){
                    alert("请选择图片！！！");
                    return;
                }
                let arr = [];
                for (let file of v.fileList) {
                    arr.push(file.response);
                }
                //把多个图片的路径合并成一个字符串
                v.weibo.urls = arr.toString();
                //发出发微博的请求
                axios.post("/weibo/insert",v.weibo).then(function (response) {
                    if (response.data == 1){
                        location.href="/";//回到首页
                    }else{
                        alert("请先登录");
                        location.href = "/login.html";
                    }
                })
            },
            handleSuccess(reaponse,file,fileList){
                v.fileList = fileList;
            },
            handleRemove(file, fileList) {
                v.fileList = fileList;
                console.log(file, fileList);
                //发出删除文件请求
                axios.get("/remove?url="+file.response).then(function (response) {
                    console.log("服务器文件已被删除！！");
                })
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            }
        }
    })
</script>
</html>